home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / mawk10.zip / MATHERR.C < prev    next >
C/C++ Source or Header  |  1991-10-05  |  4KB  |  201 lines

  1.  
  2. /********************************************
  3. matherr.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13. /*$Log:    matherr.c,v $
  14.  * Revision 3.5.1.1  91/09/14  17:23:44  brennan
  15.  * VERSION 1.0
  16.  * 
  17.  * Revision 3.5  91/08/16  11:02:09  brennan
  18.  * Carl's addition of SW_FP_CHECK for V7 XNX23A
  19.  * 
  20.  * Revision 3.4  91/08/13  06:51:46  brennan
  21.  * VERSION .9994
  22.  * 
  23.  * Revision 3.3  91/06/28  04:17:02  brennan
  24.  * VERSION 0.999
  25.  * 
  26.  * Revision 3.2  91/06/19  10:23:49  brennan
  27.  * changes for xenix_r2, call this version 0.997
  28.  * 
  29.  * Revision 3.1  91/06/07  10:27:53  brennan
  30.  * VERSION 0.995
  31.  * 
  32.  * Revision 2.3  91/06/06  07:50:43  brennan
  33.  * added infnan for libm functions on bsd43_vax
  34.  * 
  35.  * Revision 2.2  91/05/16  12:19:59  brennan
  36.  * cleanup of machine dependencies
  37.  * 
  38.  * Revision 2.1  91/04/08  08:23:31  brennan
  39.  * VERSION 0.97
  40.  * 
  41. */
  42.  
  43. #include  "mawk.h"
  44. #include  <math.h>
  45.  
  46. #if   FPE_TRAPS_ON
  47. #include <signal.h>
  48.  
  49. /* machine dependent changes might be needed here */
  50.  
  51. static void  fpe_catch( signal, why)
  52.   int signal, why ;
  53. {
  54.  
  55. #if   NOINFO_SIGFPE
  56.  /* some systems give no hook to find out what the exception
  57.     was -- stuff like this is why people still use fortran 
  58.  
  59.     If this fits, #define NOINFO_SIGFPE 1 in  your config.h
  60. */
  61.   rt_error("floating point exception, probably overflow") ;
  62. #else
  63.  
  64.   switch(why)
  65.   {
  66.     case FPE_ZERODIVIDE :
  67.        rt_error("division by zero") ;
  68.  
  69.     case FPE_OVERFLOW  :
  70.        rt_error("floating point overflow") ;
  71.  
  72.     default :
  73.       rt_error("floating point exception") ;
  74.   }
  75. #endif  
  76. }
  77.  
  78. void   fpe_init()
  79. { (void) signal(SIGFPE, fpe_catch) ; }
  80.  
  81. #else  /* FPE_TRAPS_ON==0 */
  82.  
  83. void  fpe_init()
  84. {
  85.   TURN_OFF_FPE_TRAPS() ;
  86. }
  87. #endif
  88.  
  89. #if  HAVE_MATHERR
  90.  
  91. #if  ! FPE_TRAPS_ON
  92.  
  93. /* If we are not trapping math errors, we will shutup the library calls
  94. */
  95.  
  96. int  matherr( e )
  97.   struct exception *e ;
  98. { return 1 ; } 
  99.  
  100. #else   /* print error message and exit */
  101.  
  102. int matherr( e )
  103.   struct exception  *e ;
  104. { char *error ;
  105.  
  106.   switch( e->type )
  107.   {
  108.     case  DOMAIN :
  109.     case  SING :
  110.             error = "domain error" ;
  111.             break ;
  112.  
  113.     case  OVERFLOW :
  114.             error = "overflow" ;
  115.             break ;
  116.  
  117.     case  TLOSS :
  118.     case  PLOSS :
  119.             error = "loss of significance" ;
  120.             break ;
  121.  
  122.     case  UNDERFLOW :
  123.             e->retval = 0.0 ;
  124.             return  1 ;  /* ignore it */
  125.   }
  126.  
  127.   if ( strcmp(e->name, "atan2") == 0 )
  128.       rt_error("atan2(%g,%g) : %s" ,
  129.          e->arg1, e->arg2, error ) ;
  130.   else
  131.       rt_error("%s(%g) : %s" , e->name, e->arg1, error) ;
  132.  
  133.   /* won't get here */
  134.   return 0 ;
  135. }
  136. #endif   /* FPE_TRAPS */
  137.  
  138. #endif   /*  HAVE_MATHERR */
  139.  
  140.  
  141. /* this is how one gets the libm calls to do the right
  142. thing on bsd43_vax
  143. */
  144.  
  145. #ifdef   BSD43_VAX
  146.  
  147. #include <errno.h>
  148.  
  149. double infnan( arg )
  150.   int arg ;
  151. {
  152.   switch(arg)
  153.   {
  154.     case  ERANGE : errno = ERANGE ; return HUGE ;
  155.     case -ERANGE : errno = EDOM ; return -HUGE ;
  156.     default :  errno = EDOM ; 
  157.   }
  158.   return 0.0 ;
  159. }
  160.  
  161. #endif  /* BSD43_VAX */
  162.  
  163. /* This routine is for XENIX-68K 2.3A.
  164.     Error check routine to be called after fp arithmetic.
  165. */
  166.  
  167. #if SW_FP_CHECK
  168. /* Definitions of bit values in iserr() return value */
  169.  
  170. #define OVFLOW        2
  171. #define UFLOW        4
  172. #define ZERODIV        8
  173. #define OVFLFIX        32
  174. #define INFNAN        64
  175.  
  176. void
  177. fpcheck()
  178. {
  179.     register int fperrval ;
  180.     char *errdesc ;
  181.  
  182.     if ((fperrval = iserr()) == 0)
  183.         return ;    /* no error */
  184.  
  185.     errdesc = (char *) 0 ;
  186.  
  187.     if (fperrval & INFNAN)
  188.         errdesc = "arg is infinity or NAN" ;
  189.     else if (fperrval & ZERODIV)
  190.         errdesc = "division by zero" ;
  191.     else if (fperrval & OVFLOW)
  192.         errdesc = "overflow" ;
  193.     else if (fperrval & UFLOW)
  194.         ;        /* ignored */
  195.  
  196.     if (errdesc)
  197.         rt_error("%s", errdesc) ;
  198. }
  199.  
  200. #endif
  201.